package dao;

// Generated 2 juin 2012 17:41:31 by Hibernate Tools 3.4.0.CR1

import static org.hibernate.criterion.Example.create;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import beans.VolReel;
import dao.interfaces.IDaoVolReel;

/**
 * Home object for domain model class VolReel.
 * @see beans.VolReel
 * @author Hibernate Tools
 */
public class DaoVolReel implements IDaoVolReel {

	private static final Log log = LogFactory.getLog(DaoVolReel.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoVolReel#persist(beans.VolReel)
	 */
	@Override
	public void persist(VolReel transientInstance) {
		log.debug("persisting VolReel instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(VolReel instance) {
		log.debug("attaching dirty VolReel instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(VolReel instance) {
		log.debug("attaching clean VolReel instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoVolReel#delete(beans.VolReel)
	 */
	@Override
	public void delete(VolReel persistentInstance) {
		log.debug("deleting VolReel instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoVolReel#merge(beans.VolReel)
	 */
	@Override
	public VolReel merge(VolReel detachedInstance) {
		log.debug("merging VolReel instance");
		try {
			VolReel result = (VolReel) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoVolReel#findById(beans.VolReelId)
	 */
	@Override
	public VolReel findById(beans.VolReelId id) {
		log.debug("getting VolReel instance with id: " + id);
		try {
			VolReel instance = (VolReel) sessionFactory.getCurrentSession()
					.get("beans.VolReel", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<VolReel> findByExample(VolReel instance) {
		log.debug("finding VolReel instance by example");
		try {
			List<VolReel> results = (List<VolReel>) sessionFactory
					.getCurrentSession().createCriteria("beans.VolReel")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List<VolReel> getAll() {
		Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from VolReel"); 
		List<VolReel> liste = q.list();
		return liste;
	}

	@Override
	public List<VolReel> findByPlace(String villeDepart, String villeArrivee,
			Date depart) {
		List<VolReel> listeBons = new ArrayList<VolReel>();
		Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from VolReel"); 
		
		List<VolReel> liste = q.list();
		for (VolReel volReel : liste) {
			/*System.out.println("-------");
			System.out.println("depart = "+volReel.getVolTheorique().getAeroportByAerIdPartirDe().getAerId());
			System.out.println("depart = "+volReel.getVolTheorique().getAeroportByAerIdPartirDe().getAerId());
			*/
			if(volReel.getVolTheorique().getAeroportByAerIdPartirDe().getAerId().equals(villeDepart)
			&&		
			volReel.getVolTheorique().getAeroportByAerIdAllerA().getAerId().equals(villeArrivee)
			){
				listeBons.add(volReel);
			}
			
		}
		
		return listeBons;
	}

	@Override
	public VolReel findBy(String idReel, String idTh) {
	
		List<VolReel> listeBons = new ArrayList<VolReel>();
		Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from VolReel"); 
		
		List<VolReel> liste = q.list();
		
		for (VolReel volReel : liste) {
		
			if((volReel.getId().getVolReNum().equals(idReel))
					&&(volReel.getId().getVolThNum().equals(idTh) ))
			{
				return volReel;
				
			}
			
		}
		
		return null;
	}
	
	
}
